package org.nanotek.lucene.index.search;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiFields;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.CollectionStatistics;
import org.apache.lucene.search.IndexSearcher;
import org.nanotek.cms.domain.lucene.FieldStat;
import org.nanotek.cms.domain.lucene.Index;
import org.nanotek.cms.domain.lucene.IndexStats;
import org.nanotek.cms.domain.lucene.TermStats;
import org.nanotek.dao.DAO;
import org.nanotek.lucene.query.QueryContext;
import org.nanotek.lucene.query.QueryResult;

public class IndexSearchService implements SearchService {
	
	private Provider resultsProvider; 
	private IndexSearcherProvider searchProvider; 
	private DAO<Index> indexDAO; 
	
	public IndexSearchService(){} 
	
	public List<Index> getIndexList()
	{ 
		return getIndexList (0); 
	}
	
	private List<Index> getIndexList(int i) {
		return indexDAO.listRecords(i, 200);
	}

	public QueryResult searchIndex(QueryContext queryContext) throws Exception 
	{ 
		return resultsProvider.search(queryContext);
	}
	
	public QueryResult searchIndex(String searchPhrase) throws Exception 
	{ 
		return resultsProvider.search(searchPhrase);
	}

	public QueryResult searchIndex(Index index , String searchPhrase) throws Exception 
	{ 
		return resultsProvider.search(index, searchPhrase);
	}
	
	public List<TermStats> getTermStats (Index index, String... terms) throws Exception
	{ 
		List<Term> termList = new ArrayList<>();
		for (String term : terms) 
		{ 
			Term luceneTerm =new Term("TextData" , term); 
			termList.add(luceneTerm);
		}
		List<TermStats> termStats = searchProvider.getTermStats (index, termList);
		return termStats; 
	}
	
	public IndexStats getIndexStats (Index index) throws Exception 
	{ 
		IndexStats indexStats = new IndexStats(); 
		IndexSearcher indexSearcher = searchProvider.getIndexSearcher(index); 
		CollectionStatistics stats = indexSearcher.collectionStatistics("TextData");
		indexStats.setSumDocFreq(stats.sumDocFreq()); 
		indexStats.setStatsFieldCount(stats.docCount()); 
		indexStats.setSumTotalTermFreq(stats.sumTotalTermFreq());
		return indexStats; 
	}
	
	public FieldStat getFieldStat (Index index , String field) throws Exception 
	{ 
		FieldStat stat = searchProvider.getFieldStat(index, field); 
		return stat; 
	}
	
	public Provider getResultsProvider() {
		return resultsProvider;
	}

	public void setResultsProvider(Provider resultsProvider) {
		this.resultsProvider = resultsProvider;
	}

	public IndexSearcherProvider getSearchProvider() {
		return searchProvider;
	}

	public void setSearchProvider(IndexSearcherProvider searchProvider) {
		this.searchProvider = searchProvider;
	}

	public DAO<Index> getIndexDAO() {
		return indexDAO;
	}

	public void setIndexDAO(DAO<Index> indexDAO) {
		this.indexDAO = indexDAO;
	}

	public QueryResult search(String searchPhrase) throws Exception {
		assert (resultsProvider !=null);
		return resultsProvider.search(searchPhrase);
	}

	public QueryResult search(Index index, String searchPhrase)
			throws Exception {
		assert (resultsProvider !=null);
		return resultsProvider.search(index, searchPhrase);
	}

	public QueryResult search(QueryContext queryContext) throws Exception {
		assert (resultsProvider !=null);
		return resultsProvider.search(queryContext);
	}
	
}
